package com.Arsanesia.TempleRush.Views.Screens;

import com.Arsanesia.TempleRush.TempleRush;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.OrbModeConfigurationListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.SoundConfigurationListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.TutorialConfigurationListener;
import com.Arsanesia.TempleRush.Controllers.Listeners.ButtonListeners.VibrationConfigurationListener;
import com.Arsanesia.TempleRush.Models.GameConfiguration;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class PreferenceScreen extends AbstractScreen {
	private final String TUTORIAL = "Tutorial";
	private final String SOUND = "Sound";
	private final String VIBRATION = "Vibration";
	private final String ORBMODE = "Orb Mode";
	private final float CHECKBOXY_THRESHOLD = 20;
	private final float CHECKBOX_SIDE_MARGIN = 20;
	private Image background;
	private TextBounds tutorialbound;
	private TextBounds soundbound;
	private TextBounds vibrationbound;
	private TextBounds orbmodebound;
	private float textX;
	private float tutorialY;
	private float soundY;
	private float vibrationY;
	private float orbmodeY;
	
	
	private ImageButton tutorialButton;
	private ImageButton soundButton;
	private ImageButton vibrationButton;
	private ImageButton orbButton;
	
	public TextureRegionDrawable makeNewTextureRegionDrawable(TextureRegion tr){
		return new TextureRegionDrawable(new TextureRegion (tr));
	}
	public PreferenceScreen(TempleRush game) {
		super(game);
		atlas = new TextureAtlas(Gdx.files.internal("Images/Preferences/preferences.atlas"));
		//create background image
		background= new Image(new TextureRegionDrawable(new TextureRegion(atlas.createSprite("background"))));
		
		
		//initialize text position
		textX = 100;
		soundY= VIEWPORT_DEFAULTHEIGHT-260; 		
		vibrationY = soundY - 55;
		tutorialY= vibrationY - 55;
		orbmodeY = tutorialY- 50;
		//initialize text bounds
		tutorialbound = font.getBounds(TUTORIAL);
		soundbound = font.getBounds(SOUND);
		vibrationbound = font.getBounds(VIBRATION);
		orbmodebound= font.getBounds(ORBMODE);
		
		float checkboxX = textX+vibrationbound.width+CHECKBOX_SIDE_MARGIN;
		
		Array<Sprite> buttonsprites = new Array<Sprite> (atlas.createSprites("tutorial"));
		//initialize view elements
		tutorialButton= new ImageButton(makeNewTextureRegionDrawable(buttonsprites.get(0)), 
				makeNewTextureRegionDrawable(buttonsprites.get(0)), 
				makeNewTextureRegionDrawable(buttonsprites.get(1)));
		tutorialButton.addListener(new TutorialConfigurationListener());
		tutorialButton.setChecked(!GameConfiguration.isTutorial());
		tutorialButton.setPosition(checkboxX, tutorialY-CHECKBOXY_THRESHOLD);
		
		buttonsprites = new Array<Sprite> (atlas.createSprites("sound"));
		soundButton= new ImageButton(makeNewTextureRegionDrawable(buttonsprites.get(0)), 
				makeNewTextureRegionDrawable(buttonsprites.get(0)), 
				makeNewTextureRegionDrawable(buttonsprites.get(1)));
		soundButton.addListener(new SoundConfigurationListener());
		soundButton.setChecked(!GameConfiguration.isSoundEnabled());
		soundButton.setPosition(checkboxX, soundY-CHECKBOXY_THRESHOLD);
		
		buttonsprites = new Array<Sprite> (atlas.createSprites("vibration"));
		vibrationButton= new ImageButton(makeNewTextureRegionDrawable(buttonsprites.get(0)), 
				makeNewTextureRegionDrawable(buttonsprites.get(0)), 
				makeNewTextureRegionDrawable(buttonsprites.get(1)));
		vibrationButton.addListener(new VibrationConfigurationListener());
		vibrationButton.setChecked(!GameConfiguration.isVibrate());
		vibrationButton.setPosition(checkboxX, vibrationY-CHECKBOXY_THRESHOLD);
		
		buttonsprites = new Array<Sprite> (atlas.createSprites("orb"));
		orbButton= new ImageButton(makeNewTextureRegionDrawable(buttonsprites.get(0)), 
				makeNewTextureRegionDrawable(buttonsprites.get(0)), 
				makeNewTextureRegionDrawable(buttonsprites.get(1)));
		orbButton.setPosition(checkboxX+10, orbmodeY-CHECKBOXY_THRESHOLD);
		orbButton.setChecked(!GameConfiguration.isOrbMode());
		orbButton.addListener(new OrbModeConfigurationListener());
		
	}
	
	@Override
	public void show(){
		super.show();
		this.addActor(background);
		this.addActor(tutorialButton);
		this.addActor(soundButton);
		this.addActor(vibrationButton);
		
		//if orbmode available then show the preferences
		if(GameConfiguration.isOrbModeAvailable())
			this.addActor(orbButton);
		
	}
	
	@Override
	public void render(float delta){
		super.render(delta);
		
		
	}

}
